import os
import re
import numpy as np
import pandas as pd
import joblib

# os.chdir(r'D:\项目\8.16_模型上线_民生卡借转贷\0.模型打分')
os.getcwd()

os.listdir()

def model_score(data,model,points0 = 45,pdo = -23,odds0 = 0.133):
    # 数据整理
    feature_name = model.booster_.feature_name() # 取出入模变量
    data_final = data[feature_name] # 进行打分的数据
    data_cusnum = data[['cus_num','name','cell','id']] # 取出cus_num、id等
    # 计算分数
    B = pdo / np.log(2)
    A = points0 + B * np.log(odds0)
    pred = model.predict_proba(data_final)[:,1]
    score = np.around(A - B * np.log(pred/(1 - pred)))
    score[score <= 0] = 0
    score[score >= 100] = 100
    # 分数与cus_num等字段拼接
    result = pd.concat([data_cusnum,pd.DataFrame(score,columns = ['score'])],axis = 1)
    return result


data = pd.read_csv('test_3000_dts_result.csv',encoding = 'utf-8')
model = joblib.load('msk_jzd_lgbm.pkl')

result = model_score(data,model = model,points0 = 45,pdo = -23,odds0 = 0.133)

result.to_csv('模型打分结果.csv',encoding = 'utf-8',index = False)